package com.example.myfirstapplication

import android.app.ActivityGroup
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.SeekBar
import android.widget.TabHost
import com.example.myfirstapplication.utils.Tag
import kotlinx.android.synthetic.main.activity_common_widget2.*
import java.util.*

//class CommonWidget2 : AppCompatActivity(), View.OnClickListener {
class CommonWidget2 : ActivityGroup(), View.OnClickListener {

    private var dateStr: String = ""
    private var timeStr: String = ""
    private var time: String = ""
    private var year: Int = 0
    private var month: Int = 0
    private var day: Int = 0
    private var hour: Int = 0
    private var minute: Int = 0
    private var second: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_common_widget2)
        ll_datePicker.setOnClickListener(this)
        ll_timePicker.setOnClickListener(this)
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                label_seekBar.text = "当前进度：${progress}%"
                Log.i(Tag, "seek $progress")
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                Log.i(Tag, "seek start")
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                Log.i(Tag, "seek stop")
            }
        })
        ratingBar.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
            label_ratingBar.text = "您给出了${rating}星评价"
        }
        tabhost.setup(this.localActivityManager)
//创建Tag
        val spec1: TabHost.TabSpec = tabhost.newTabSpec("SeekBar")
//设置标签文字
        spec1.setIndicator("SeekBar控件").setContent(R.id.tab1_ll)
//添加到host中
        tabhost.addTab(spec1)
        val spec2: TabHost.TabSpec = tabhost.newTabSpec("RatingBar")
        spec2.setIndicator("RatingBar控件").setContent(R.id.tab2_ll)
        tabhost.addTab(spec2)
        val spec3: TabHost.TabSpec = tabhost.newTabSpec("ListView")
        val listViewIntent = Intent(this, ListViewActivity::class.java)
        spec3.setIndicator("ListView控件").setContent(listViewIntent)
        tabhost.addTab(spec3)
        val spec4: TabHost.TabSpec = tabhost.newTabSpec("RecyclerView")
        val recyclerViewIntent = Intent(this, RecyclerViewActivity::class.java)
        spec4.setIndicator("RecyclerView控件").setContent(recyclerViewIntent)
        tabhost.addTab(spec4)
    }

    private fun initDateTime() {
        val ca = Calendar.getInstance()
        year = ca.get(Calendar.YEAR)
        month = ca.get(Calendar.MONTH) + 1
        day = ca.get(Calendar.DAY_OF_MONTH)
        hour = ca.get(Calendar.HOUR_OF_DAY)
        minute = ca.get(Calendar.MINUTE)
        second = ca.get(Calendar.SECOND)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_datePicker -> {
                DatePickerDialog(
                    this,
                    //AlertDialog.THEME_HOLO_DARK,
                    DatePickerDialog.OnDateSetListener { view, selectyear, selectmonth, selectdayOfMonth ->
                        if (dateStr.isNotEmpty()) {
                            dateStr = ""
                        }
                        dateStr = "${selectyear}年${selectmonth}月${selectdayOfMonth}日"
                        txtView_datePicker.text = dateStr
                    },
                    year, month, day
                ).show()
            }
            R.id.ll_timePicker -> {
                TimePickerDialog(
                    this,
                    TimePickerDialog.OnTimeSetListener { view, selectedHour, selectedMinute ->
                        if (timeStr.isNotEmpty()) {
                            timeStr = ""
                        }
                        val h = if (selectedHour < 10) {
                            "0".plus(selectedHour)
                        } else {
                            selectedHour
                        }
                        val m = if (selectedMinute < 10) {
                            "0".plus(selectedMinute)
                        } else {
                            selectedMinute
                        }
                        timeStr = "${h}:${m}:00"
                        txtView_timePicker.text = timeStr
                    },
                    hour, minute, true
                ).show()
            }
        }
    }
}
